﻿$(function () {
    datePicker("#AvailableTime1");
    datePicker("#AvailableTime2");
    var productName = $(".HotelName");
    var productID = $(".HotelID");
    var ownerID = $(".OwnerID");
    var childID = $(".ProductChildID");
    for (var i = 0; i < productName.length; i++) {
        setCombobox(productName.eq(i), productID.eq(i), this, ownerID.eq(i), childID.eq(i));
    }
    var ticketOwnerID = $(".TicketOwnerID");
    var sceneryID = $(".SceneryID");
    var sceneryName = $(".SceneryName");
    var ticketchildID = $(".TicketProductChildID");
    for (var k = 0; k < sceneryName.length; k++) {

        setsceneryCombobox(sceneryName.eq(k), sceneryID.eq(k), this, ticketOwnerID.eq(k), ticketchildID.eq(k));
    }
    $("input[name='ProductCount']").bind("keyup ", function () {
        updateMoney(this);
    })
    $("input[name='ProductPrice']").bind("keyup", function () {
        updateMoney1(this);
    })
})


function setCombobox(txtID, valID, src, owner, childID) {
    txtID.combobox({
        ColumnsMapsNodeName: 'HotelID|Name|OwnerID|RoomName|HotelRoomID',
        TagObject: null,
        MustMatching: false,
        ListWidth: 400,
        DisplayHead: true,
        ListType: ListType.Combobox,
        DataType: DataType.Xml,
        DataLoadOnce: false,
        RemoteUrl: "/data/getxmldata?datatype=hotelroom&keyword=",
        TextFormat: 'V1',
        ValueFormat: 'V0,V1,V2,V3,V4',
        OnItemClickEvent: function (src, data) {
            var HotelName = data.json.Name + "<" + data.json.RoomName + ">";
            valID.val(data.json.HotelID);
            txtID.val(HotelName);
            owner.val(data.json.OwnerID);
            childID.val(data.json.HotelRoomID);
        },
        HeadColumn: [
            { name: "编号", width: 60, display: false },
            { name: "酒店名称" },
            { name: "OwnerID", display: true },
            { name: "房间名称" },
             { display: false }
            ],
        KeyWordColumn: ''
    });
}
function setsceneryCombobox(txtID, valID, src, owner, childID) {
    txtID.combobox({
        ColumnsMapsNodeName: 'SceneryID|SceneryName|OwnerID|TicketName|SceneryTicketID',
        TagObject: null,
        MustMatching: false,
        ListWidth: 400,
        DisplayHead: true,
        ListType: ListType.Combobox,
        DataType: DataType.Xml,
        DataLoadOnce: false,
        RemoteUrl: "/data/getxmldata?datatype=sceneryTicket&keyword=",
        TextFormat: 'V1',
        ValueFormat: 'V0,V1,V2,V3,V4',
        OnItemClickEvent: function (src, data) {
            valID.val(data.json.SceneryID);
            var TicketName = data.json.SceneryName + "<" + data.json.TicketName + ">";
            txtID.val(TicketName);
            owner.val(data.json.OwnerID);
            childID.val(data.json.SceneryTicketID);
        },
        HeadColumn: [
            { name: "编号", width: 60, display: false },
            { name: "景点名称" },
            { name: "OwnerID", display: false },
            { name: "门票名称" },
            { display: false }
            ],
        KeyWordColumn: ''
    });
}
function addRow(tbl, src) {
    var tr = $("#" + tbl + " tr").eq(2).clone();
    $("input[name=ProductName]", tr).val("");
    $("input[name=ProductCount]", tr).val("0");
    $("input[name=ProductPrice]", tr).val("0");
    $(".Total", tr).text("0");
    tr.show();
    tr.insertAfter("#tblHotel");
    var txt = $("input[name=ProductName]", tr);
    var val = $("input[name=ProductID]", tr);
    var owner = $("input[name=OwnerID]", tr);
    var child = $("input[name=ProductChildID]", tr);
    setCombobox(txt, val, this, owner, child);
    $("input[name=ProductCount],input[name=ProductPrice]", tr).bind("keyup ", function () {
        updateMoney(this);
    });
}
function addTicketRow(tbl, src) {
    var tr = $("#tblTicket").clone();
    $("input[name=ProductName]", tr).val("");
    $("input[name=ProductCount]", tr).val("0");
    $("input[name=ProductPrice]", tr).val("0");
    $(".Total", tr).text("0");
    tr.show();
    tr.insertAfter("#tblTicket");
    var txt = $("input[name=ProductName]", tr);
    var val = $("input[name=ProductID]", tr);
    var owner = $("input[name=OwnerID]", tr);
    var child = $("input[name=ProductChildID]", tr);
    setsceneryCombobox(txt, val, this, owner, child);
    $("input[name=ProductCount],input[name=ProductPrice]", tr).bind("keyup ", function () {
        updateMoney1(this);
    });
}
function delRow(src) {
    $.Msg.confirmDelete(function () {
        var row = $(src).parent().parent();
        row.remove();

    });
}
function updateCount(act, src) {
    var tr = $(src);
    while (tr.attr("tagName") != "TR") {
        tr = tr.parent();
    }
    var price = $(".Price", tr).val();
    var spTotal = $(".Total", tr);
    if (act == "add") {
        var oCount = $(src).next();
        var count = parseInt(oCount.val());
        count++;
        oCount.val(count);
        var m = count * price;
        spTotal.text(m);
    } else {
        var oCount = $(src).prev();
        var count = parseInt(oCount.val());
        count--;
        oCount.val(count);
        spTotal.text(count * price);
    }
}
function updateMoney(src) {
    var tr = $(src);
    while (tr.attr("tagName") != "TR") {
        tr = tr.parent();
    }
    var price = $(".Price", tr).val();
    var spTotal = $(".Total", tr);
    var count = $("input[name=ProductCount]", tr).val();
    spTotal.text(count * price);
}
function updateMoney1(src) {
    var tr = $(src);
    while (tr.attr("tagName") != "TR") {
        tr = tr.parent();
    }
    var spTotal = $(".Total", tr);
    var count = $("input[name=ProductCount]", tr).val(); ;
    var price = $(src).val();
    spTotal.text(count * price);
}